# -*- coding: utf-8 -*-
# @Time    : 2022/4/14 14:40
# @File : key_word.py
# @Author  : Yang
from time import sleep
from pywinauto import Application
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait


class KeyWords():
    # 创建浏览器
    def __init__(self, driver):
        self.driver = driver
        self.driver.implicitly_wait(10)

    # 打开网页
    def open_url(self, url):
        self.driver.get(url)

    # 关闭浏览器
    def quit_browser(self):
        self.driver.quit()

    # 定位元素
    def locator(self, name, value):
        return self.driver.find_element(name, value)

    # 以list方式传入元素定位
    def locator_list(self, el_list):
        el = self.driver.find_element(el_list[0], el_list[1])
        self.locator_station(el)
        return el

    # 通过传入组元素定位
    def locator_list_group(self, list1):
        return self.driver.find_element(list1[0], list1[1])

    # 标注当前定位
    def locator_station(self, element):
        self.driver.execute_script(
            "arguments[0].setAttribute('style', arguments[1]);",
            element,
            "border: 2px solid green;"  # 边框border:2px; red红色
        )

    # 点击事件
    def click(self, el_list):
        self.locator_list(el_list).click()

    # 输入内容
    def input(self, el_list, txt):
        el = self.driver.find_element(el_list[0], el_list[1])
        el.clear()
        el.send_keys(txt)

    # 强制等待
    def wait_time(self, time_):
        sleep(time_)

    # 获取文本内容
    def get_text(self, path):
        return self.locator('xpath', path).text

    # 切换句柄
    def switch_handle(self, num):
        handles = self.driver.window_handles
        self.driver.switch_to.window(handles[num])
        print(self.driver.title)

    # 切换frame
    def switch_frame(self, value, name=None):
        if name in None:
            self.driver.switch_to.frame(value)
        else:
            self.driver.switch_to.frame(self.locator(name, value))

    # 鼠标悬停
    def mouse_hold(self, url):
        btn = self.driver.find_elements('xpath', url)[0]
        action = ActionChains(self.driver)
        action.click_and_hold(btn).perform()

    # 切换主框架
    def switch_default_frame(self):
        self.driver.switch_to.default_content()

    # 显示等待
    def web_el_wait(self, el_list):
        return WebDriverWait(self.driver, 10, 0.5).until(
            lambda el: self.locator(el_list[0], el_list[1]), message='元素查找失败'
        )

    # 断言文本信息
    def assert_text(self, el_list, expect):
        try:
            reality = self.locator_list(el_list).text
            assert expect == reality, '断言失败，实际结果为：{}'.format(reality)
            return True
        except Exception as e:
            print('断言失败信息：%s', e)
            return False

    # 获取元素定位下标
    def get_el_num(self, num1, attr, content):
        """
        :param num1: 需要查找的元素位于第几列
        :param attr: 当前元素的标签属性
        :param content: 查找元素的text
        :return:
        """

        # 获取相同标签的所有元素
        pass

    # 去除属性
    def remove_attribute(self, class_name, num, attr):
        js = 'document.getElementsByClassName("{0}")[{1}].removeAttribute("{2}")'.format(class_name, num, attr)
        self.driver.execute_script(js)

    # 上传文件
    def upload_file(self, path):
        app = Application()
        app = app.connect(title='打开', class_name='#32770')
        app['打开']['Edit1'].set_edit_text(path)
        self.wait_time(0.5)
        app['打开']['Button'].click_input()
        # app['打开']['Button'].click()

